<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Model\ShopGoodModel;
use App\Model\ShopOrderFstModel;
use App\Model\StoreGoodModel;
use App\Model\UserModel;
use App\Model\ConfigModel;
use App\Model\UserLoginModel;
use App\Model\StoreGoodLog;
use App\Model\SkuModel;
use App\Model\ShopOrderFs;
use App\Model\ShopOrder;

use App\Model\StoreModel as Store;
use App\Model\StoreGoodModel as StoreGood;
use App\Model\ShopOrderFstModel as ShopOrderFst;
use Illuminate\Support\Facades\DB;
use Mockery\Expectation;


class StoreController extends Controller
{
	//我的云仓库
	public function list(){
		$user_store = Store::where('uid',request()->uid)->first();
		if(!$user_store){
			return ['code'=>0,'msg'=>retStrInfo(52)];
		}
		if($user_store->state == 2){
			return ['code'=>0,'msg'=>retStrInfo(53)];
		}
		//统计库存数量
		$data['user_store_num'] = StoreGood::where('sid',$user_store->id)->sum('num');

		//库存
		$store_num = StoreGood::where('sid',$user_store->id)->get();
		$user_store_coin = 0;
		foreach($store_num as $k=>$v){
			$user_store_coin += $v['gsell_price']*$v['num'];
		}

		//库存总额
		$data['user_store_sell'] = number_format($user_store_coin,2,".","");
		//销售额
		$user_store_sids = StoreGoodModel::where('sid',$user_store['id'])->get();
		$sids = '';
		foreach ($user_store_sids as $k=>$v){
			$sids .= $v['id'].',';
		}
		$sids = explode(',',trim($sids,','));
		$user_store_slist = ShopOrderFstModel::whereIn('sid',$sids)->get();
		$sell = 0;
		//盈利
		$profit = 0;
		$profit_ratio = ConfigModel::getValue('profit');
		foreach ($user_store_slist as $k=>$v){
			$sell += $v['sell_price']*$v['num'];
			$shop = StoreGoodModel::where('sid',$v['sid'])->where('gid',$v['gid'])->first();
			$profit += bcmul(bcadd($shop['gsell_price'],$v['sell_price']*$profit_ratio,2),$v['num'],2);
		}

		$data['user_store_coin'] = number_format($sell,2,".","");

		$data['user_store_profit'] = number_format($profit,2,".","");

		//库存商品
		$data['store_good'] = StoreGood::where('sid',$user_store['id'])->select('id','gid','gsell_price','gcost_price','attr_name','num','sale_num')->get();
		//已售商品
		$data['store_good_sell'] = StoreGood::where('sid',$user_store['id'])->select('id','gid','gsell_price','gcost_price','attr_name','sale_num')->where('sale_num','<>',0)->get();
		foreach($data['store_good'] as $k=>$v){
			$goods = ShopGoodModel::where('id',$v['gid'])->first();
			$v['name'] = $goods['name'];
			$v['gsell_price'] = number_format($v['gsell_price'],'2',".","");
			$v['gcost_price'] = number_format($v['gcost_price'],'2',".","");

			if($goods['oss_pic']){
				$v['picname'] = $goods['oss_pic'];
			}else{
				$v['picname'] = "http://".$_SERVER['HTTP_HOST'].$goods['picpath'].$goods['picname'];
			}
			$v['profit'] = $v['gsell_price']-$v['gcost_price'];
			if(empty($v['attr_name'])){
				$v['attr_name'] = '无规格';
			}
		}

		foreach($data['store_good_sell'] as $k=>$v){
			$goods = ShopGoodModel::where('id',$v['gid'])->first();
			$v['name'] = $goods['name'];
			$v['gsell_price'] = number_format($v['gsell_price'],'2',".","");
			$v['gcost_price'] = number_format($v['gcost_price'],'2',".","");
			if($goods['oss_pic']){
				$v['picname'] = $goods['oss_pic'];
			}else{
				$v['picname'] = "http://".$_SERVER['HTTP_HOST'].$goods['picpath'].$goods['picname'];
			}
			$v['profit'] = $v['gsell_price']-$v['gcost_price'];
			if(empty($v['attr_name'])){
				$v['attr_name'] = '无规格';
			}
		}

		return ['code'=>1,'msg'=>'返回数据','data'=>$data];
	}

	//用户的运仓号
	public function index(){
		$user = UserModel::where('id',request()->uid)->first();
		$data['pic'] = "http://".$_SERVER['HTTP_HOST']."/upload/invite/".$user['invite_code'].".png";
		$data['name'] = $user['user'];
//		$data['headpic'] = $user['picname'];
//		if(!$user['picname']){
//			$data['headpic'] = "http://" . $_SERVER['HTTP_HOST'] . "/user.png";
//		}

		//用户第三方登录
		$party = UserLoginModel::where('uid',request()->uid)->first();
		if(empty($user['picname'])){
			if(empty($party['picname'])){
				$data['headpic'] = "http://".$_SERVER['HTTP_HOST']."/user.png";
			}else{
				$data['headpic'] = $party['picname'];
			}
		}else{
			$data['headpic'] = "http://".$_SERVER['HTTP_HOST'].$user['picname'];
		}


		return ['code'=>1,'data'=>$data];
	}

	//入库订单
	public function add_goods(){
		//用户ID
		$uid = request()->uid;
		$arr = [];
		//入库的订单
		$list = StoreGoodLog::where('uid',$uid)->paginate(15);
		foreach($list as $k=>$v){
			$good = ShopGoodModel::where('id',$v['gid'])->first();
			$arr[$k]['attr'] = '无';
			if($v['sku_id'] > 0){
				$shop = SkuModel::where('id',$v['sku_id'])->first();
				$arr[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$shop['picpath'].$shop['picname'];
				$arr[$k]['attr'] = $shop['attr'];
			}else{
				$shop = ShopGoodModel::where('id',$v['gid'])->first();
				$arr[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$shop['picname'];
			}
			if($shop['oss_pic']){
				$arr[$k]['picname'] = $v['oss_pic'];
			}
			$arr[$k]['gname'] = $good['name'];
			$arr[$k]['id'] = $v['id'];
			$arr[$k]['sn'] = $v['sn'];
			$arr[$k]['created_at'] = date('Y-m-d H:i:s',$v->created_at->timestamp);
			$arr[$k]['sell'] = $v['sell'];
			$arr[$k]['num'] = $v['num'];
			$arr[$k]['total'] = $v['total'];
	
		}
		return ['code'=>1,'msg'=>'返回数据','data'=>$arr];
	}

	//出库订单
	public function del_goods(){
		$uid = request()->uid;
		//仓库信息
		$store = Store::where('uid',$uid)->first();
		//商品信息
		$good = ShopOrderFst::where('sid',$store['id'])->get();
		$arr = [];
		foreach($good as $k=>$v){
			//商品信息
			$list_cy = ShopGoodModel::where('id',$v['gid'])->first();
			
			//订单信息
			$list_order = ShopOrderFs::where('id',$v['fid'])->first();
			$list_f_order = ShopOrder::where('id',$list_order['oid'])->first();
			$arr[$k]['attr'] = '无';
			if($v['sku_id'] > 0){
				$list = SkuModel::where('id',$v['sku_id'])->first();
				$arr[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$list['picpath'].$list['picname'];
				$arr[$k]['attr'] = $list['attr'];
			}else{
				$list = ShopGoodModel::where('id',$v['gid'])->first();
				$arr[$k]['picname'] = "http://".$_SERVER['HTTP_HOST'].$list['picname'];
			}
			if($list['oss_pic']){
				$arr[$k]['picname'] = $list['oss_pic'];
			}
			$arr[$k]['name'] = $list_cy['name'];
			$arr[$k]['sn'] = $list_f_order['sn'];
			$arr[$k]['created_at'] = date('Y-m-d H:i:s',$v->created_at->timestamp);
		
			$arr[$k]['sell'] = $v['sell_price'];
			$arr[$k]['total'] = $v['total'];
			$arr[$k]['num'] = $v['num'];
			
		}
		return ['code'=>1,'msg'=>'返回数据','data'=>$arr];
		
	}
}